package cn.wilson.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
    //邮件的队列名
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    //短信的队列名
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    //交换机的名字
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    /**
     *     1声明交换机
     * @return
     */
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange exchangeTopicsInform(){
        //durable:交换机的持久化
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }


    /**
     *      2声明邮件额队列
     * @return
     */
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue queueInformEmail(){
        //队列的持久化
        return new Queue(QUEUE_INFORM_EMAIL,true);
    }

    /**
     *      3声明短息你的队列
     * @return
     */
    @Bean(QUEUE_INFORM_SMS)
    public Queue queueInformSms(){
        return new Queue(QUEUE_INFORM_SMS);
    }


    /**
     *      4绑定邮件队列到交换机
     * @return
     */
    @Bean
    public Binding BindingQueueFromEmail(){
        //通过调用上面的方法获取bean对象
        return BindingBuilder.bind(queueInformEmail()).to(exchangeTopicsInform()).with("email.*").noargs();
    }

    /**
     *      @Qualifier: 因为上面的bean有两个，他不知道要使用哪个bean，所以用此注解指定
     *      5绑定短信队列到交换机
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding BindingQueueFromSms(@Qualifier(QUEUE_INFORM_SMS)Queue queue,
                                       @Qualifier(EXCHANGE_TOPICS_INFORM)Exchange exchange){
        //通过调用上面的方法获取bean对象
        return BindingBuilder.bind(queue).to(exchange).with("sms.*").noargs();
    }


}
